package com.mofeng.myshop.domain.dto.user;

import com.mofeng.myshop.domain.dto.base.BaseDto;
import com.mofeng.myshop.domain.po.UserBase;
import com.mofeng.myshop.domain.po.UserInfo;
import com.mofeng.myshop.domain.po.UserPower;
import com.mofeng.myshop.domain.po.UserRole;
import com.mofeng.myshop.until.MyUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class UserDto extends BaseDto {
    @Schema(description = "头像")
    private String imageUrl;

    private String password;

    private String newPassword;

    @Schema(description = "登录来自PC还是手机")
    private String from;

    @Schema(description = "邮箱")
    private String email;

    @Schema(description = "电话")
    private String phone;

    @Schema(description = "电话/邮箱验证码")
    String phoneOrEmailCode;

    @Schema(description = "状态 1 正常 2 禁用")
    private String state;

    @Schema(description = "Steam ID")
    private String steamId;

    @Schema(description = "游戏 ID")
    private String gameId;

    @Schema(description = "qq ")
    private String qq;

    @Schema(description = "余额")
    private String money;

    @Schema(description = "消费余额")
    private String amount;

    @Schema(description = "游戏名 ")
    private String gameName;

    @Schema(description = "性别")
    private String sex;

    @Schema(description = "简介")
    private String introduction;

    @Schema(description = "地址")
    private String address;

    @Schema(description = "领取新人奖励")
    private String receiveState;

    @Schema(description = "邀请码UUID")
    private String invitationCode;

    @Schema(description = "邀请码用户")
    private Long invitationUserId;

    @Schema(description = "权限等级")
    private String powerNumber;
    private String powerId;
    @Schema(description = "权限名称")
    private String powerName;
    @Schema(description = "角色名称")
    private String roleName;
    private String roleId;
    @Schema(description = "权限折扣")
    private Float discount;


    public UserBase toEntity() {
        UserBase userBase = new UserBase();
        toBaseEntity(userBase);
        if (this.imageUrl != null) {
            userBase.setImage(this.imageUrl);
        }
        if (this.email != null) {
            userBase.setEmail(this.email);
        }
        if (this.phone != null) {
            userBase.setPhone(this.phone);
        }
        if (this.state != null) {
            userBase.setState(this.state);
        }
        if (this.password != null) {
            userBase.setPassword(this.password);
        }
        UserInfo userInfo = getInfo();
        UserPower userPower = new UserPower();
        userPower.setNumber(MyUtil.NumberUtil.asInt(this.powerNumber));
        userPower.setName(this.powerName);
        if (this.powerId != null) {
            userPower.setId(Long.parseLong(this.powerId));
        }
        UserRole userRole = new UserRole();
        userRole.setName(this.roleName);
        userRole.setDiscount(this.discount);
        if (this.roleId != null) {
            userRole.setId(Long.parseLong(this.roleId));
        }
        userBase.setInfo(userInfo);
        userBase.setPower(userPower);
        userBase.setRole(userRole);
        return userBase;
    }

    public UserInfo getInfo() {
        UserInfo userInfo = new UserInfo();
        if (!MyUtil.StringUtil.isEmpty(this.id)) {
            userInfo.setId(Long.parseLong(this.id));
        }
        if (this.steamId != null) {
            userInfo.setSteamId(this.steamId);
        }
        if (this.gameId != null) {
            userInfo.setGameId(this.gameId);
        }
        if (this.qq != null) {
            userInfo.setQq(this.qq);
        }
        if (this.gameName != null) {
            userInfo.setGameName(this.gameName);
        }
        if (this.sex != null) {
            userInfo.setSex(this.sex);
        }
        if (this.introduction != null) {
            userInfo.setIntroduction(this.introduction);
        }
        if (this.address != null) {
            userInfo.setAddress(this.address);
        }

        if (!MyUtil.StringUtil.isEmpty(this.receiveState)) {
            userInfo.setReceiveState(this.receiveState);
        }

        if (!MyUtil.StringUtil.isEmpty(this.money)) {
            userInfo.setMoney(this.money);
        }

        if (!MyUtil.StringUtil.isEmpty(this.amount)) {
            userInfo.setAmount(this.amount);
        }

        if (!MyUtil.StringUtil.isEmpty(this.invitationCode)) {
            userInfo.setInvitationCode(this.invitationCode);
        }

        if (!MyUtil.StringUtil.isEmpty(this.invitationUserId)) {
            userInfo.setInvitationUser(this.invitationUserId);
        }
        return userInfo;
    }
}
